package com.mohansever.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mohansever.req.AddArticleMReq;
import com.mohansever.service.ArticleMService;
import com.mohansever.tool.Object2Map;
import com.mohansever.util.ErrStatusEnum;
import com.mohansever.util.LoginToken;
import com.mohansever.util.PageData;
import com.mohansever.util.ResponseUtil;

@Controller
@RequestMapping(value = "/v1/sever")
public class ArticleMController extends BaseController{
	@Resource
	private ArticleMService aMservice;
	
	/**
	 * 新增文章
	 * @param request
	 * @param aaMReq
	 * @return
	 */
	@RequestMapping(value = "/addArticle", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object addArticle(HttpServletRequest request, @RequestBody AddArticleMReq aaMReq) {
		Map<String, Object> parmMap = Object2Map.beanToMap(aaMReq);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (parmIsEnable(parmMap)) {
				//登录验证
				repUtil = LoginToken.loginConfirm(aaMReq.getUserId(), aaMReq.getToken());
				if (repUtil == null) {
					repUtil = aMservice.addArticleM(aaMReq);
				}
			}else {
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return repUtil;
	}
	
	/**
	 * 删除文章
	 * @param request
	 * @param parmMap
	 * @return
	 */
	@RequestMapping(value = "/delArticle", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object delArticle(HttpServletRequest request,@RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			
			if (validataParms(new String[]{"userId","token","articleId"}, pd)) {
				
				//登录验证
				repUtil = LoginToken.loginConfirm(pd.getString("userId"), pd.getString("token"));
				if (repUtil == null) {
					repUtil = aMservice.delArticleM(pd);
				}
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {  
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return returnObject(pd, repUtil);
	}
	
	/**
	 * 修改文章
	 * @param request
	 * @param aaMReq
	 * @return
	 */
	@RequestMapping(value = "/updateArticle", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object updateArticle(HttpServletRequest request, @RequestBody AddArticleMReq aaMReq) {
		Map<String, Object> parmMap = Object2Map.beanToMap(aaMReq);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (parmIsEnable(parmMap)) {
				//登录验证
				repUtil = LoginToken.loginConfirm(aaMReq.getUserId(), aaMReq.getToken());
				if (repUtil == null) {
					repUtil = aMservice.updateArticleM(aaMReq);
				}
			}else {
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return repUtil;
	}
	
	/**
	 * 分页获取文章列表
	 * @param request
	 * @param parmMap
	 * @return
	 */
	@RequestMapping(value = "/articleList", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object getUserList(HttpServletRequest request, @RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (validataParms(new String[]{"userId","token","targetUserId","queryPage","eachPageSize"}, pd)) {
				//登录验证
				repUtil = LoginToken.loginConfirm(pd.getString("userId"), pd.getString("token"));
				if (repUtil == null) {
					repUtil = aMservice.getPageList(pd);
				}
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {  
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return returnObject(pd, repUtil);
	}
	
	/**
	 * 获取文章内容
	 * @param request
	 * @param parmMap
	 * @return
	 */
	@RequestMapping(value = "/getArticle", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object getArticle(HttpServletRequest request, @RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (validataParms(new String[]{"userId","token","articleId"}, pd)) {
				//登录验证
				repUtil = LoginToken.loginConfirm(pd.getString("userId"), pd.getString("token"));
				if (repUtil == null) {
					repUtil = aMservice.getArticle(pd);
				}
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {  
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return returnObject(pd, repUtil);
	}
	
	/**
	 * 修改文章的articleStatus
	 * @param request
	 * @param parmMap
	 * @return
	 */
	@RequestMapping(value = "/chgArticleStatus", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object chgArticleStatus(HttpServletRequest request, @RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (validataParms(new String[]{"userId","token","articleId","articleStatus"}, pd)) {
				//登录验证
				repUtil = LoginToken.loginConfirm(pd.getString("userId"), pd.getString("token"));
				if (repUtil == null) {
					repUtil = aMservice.chgArticleStatus(pd);
				}
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {  
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return returnObject(pd, repUtil);
	}
	
	/**
	 * 修改文章推荐状态
	 * @param request
	 * @param parmMap
	 * @return
	 */
	@RequestMapping(value = "/chgArticleRecommend", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object chgArticleRecommend(HttpServletRequest request, @RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (validataParms(new String[]{"userId","token","articleId","recommend"}, pd)) {
				//登录验证
				repUtil = LoginToken.loginConfirm(pd.getString("userId"), pd.getString("token"));
				if (repUtil == null) {
					repUtil = aMservice.chgArticleRecommend(pd);
				}
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {  
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return returnObject(pd, repUtil);
	}
	
	/**
	 * 修改文章的公开状态
	 * @param request
	 * @param parmMap
	 * @return
	 */
	@RequestMapping(value = "/chgArticleIsOpen", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object chgArticleIsOpen(HttpServletRequest request, @RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (validataParms(new String[]{"userId","token","articleId","isOpen"}, pd)) {
				//登录验证
				repUtil = LoginToken.loginConfirm(pd.getString("userId"), pd.getString("token"));
				if (repUtil == null) {
					repUtil = aMservice.chgArticleIsOpen(pd);
				}
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {  
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return returnObject(pd, repUtil);
	}
	
	/**
	 * 修改文章类别
	 * @param request
	 * @param parmMap
	 * @return
	 */
	@RequestMapping(value = "/chgArticleClass", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object chgArticleClass(HttpServletRequest request, @RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (validataParms(new String[]{"userId","token","articleId","articleClass"}, pd)) {
				//登录验证
				repUtil = LoginToken.loginConfirm(pd.getString("userId"), pd.getString("token"));
				if (repUtil == null) {
					repUtil = aMservice.chgArticleClass(pd);
				}
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {  
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return returnObject(pd, repUtil);
	}
}
